﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_ThongTinLopTC : System.Web.UI.Page
{
    DataClassesDataContext data = new DataClassesDataContext();

    HocVien hv = new HocVien();
    int action;
    string msltc = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlMaMonHoc.DataSource = data.tbl_MONHOCs;
            ddlMaMonHoc.DataTextField = "TenMonHoc";
            ddlMaMonHoc.DataValueField = "MsMonHoc";
            ddlMaMonHoc.DataBind();
            PageLoad();
        }
    }

    private void PageLoad()
    {
        action = int.Parse(Request.QueryString["action"]);
        if (action == 1)
        { 
        }
        if (action == 2)
        {
            msltc = Request.QueryString["MSLTC"].ToString();
            if (msltc != "")
                LoadThongTin(Request.QueryString["MSLTC"].ToString());
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Error !!!Không có mã số lớp tín chỉ');", true);
                Response.Redirect("./QuanLyLopTC.aspx");
            }
        }
    }

    private void LoadThongTin(string p)
    {
        DataClassesDataContext data = new DataClassesDataContext();
        var loptcs = from temp in data.tbl_LOPTCs
                 where temp.MsLopTC == p
                 select temp;
        foreach (tbl_LOPTC ltc in loptcs)
        {
            ddlMaMonHoc.SelectedValue = ltc.MsMonHoc;
            txtNgayBatDau.Text = ltc.NgayBatDau.Value.ToShortDateString();
            txtNgayKetThuc.Text = ltc.NgayKetThuc.Value.ToShortDateString();
        }
        txtMaLopTC.Text = p;
        txtMaLopTC.Enabled = false;
   }

    private string TuDongSinhMaHocVien()
    {
        string MaHV = "SV001";
        int a = 1;
        while (hv.KiemTraMaHV(MaHV) == true)
        {
            if (a < 9)
            {
                a++;
                MaHV = "SV00" + a;
            }
            else if (a >= 9 && a < 99)
            {
                a++;
                MaHV = "SV0" + a;
            }
            else if (a >= 99)
            {
                a++;
                MaHV = "SV" + a;
            }
        }
        return MaHV;
    }

    protected void btnSavePerson_Click(object sender, EventArgs e)
    {
        try
        {
            action = int.Parse(Request.QueryString["action"]);

            if (action == 1)
            {
                DataClassesDataContext data = new DataClassesDataContext();

                tbl_LOPTC ltc = new tbl_LOPTC();
                ltc.MsLopTC = txtMaLopTC.Text;
                ltc.MsMonHoc = ddlMaMonHoc.SelectedValue;
                ltc.NgayBatDau = DateTime.Parse(txtNgayBatDau.Text);
                ltc.NgayKetThuc = DateTime.Parse(txtNgayKetThuc.Text);

                data.tbl_LOPTCs.InsertOnSubmit(ltc);
                data.SubmitChanges();
            }
            if (action == 2)
            {
                msltc = Request.QueryString["MSLTC"].ToString();

                DataClassesDataContext data = new DataClassesDataContext();

                var SuaLTC = from p in data.tbl_LOPTCs
                            where p.MsLopTC == msltc
                            select p;


                foreach (tbl_LOPTC Sua_LTC in SuaLTC)
                {
                    Sua_LTC.MsMonHoc = ddlMaMonHoc.SelectedValue;
                    Sua_LTC.NgayBatDau = DateTime.Parse(txtNgayBatDau.Text);
                    Sua_LTC.NgayKetThuc = DateTime.Parse(txtNgayKetThuc.Text);
                }

                data.SubmitChanges();
            }
            Response.Redirect("./QuanLyLopTC.aspx");

        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Error !!!" + ex.Message.ToString() + "');", true);
        }

    }
}
